package com.waverider.promotion.biz.manager.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.waverider.promotion.biz.dao.OrderDAO;
import com.waverider.promotion.biz.dao.PromotionDAO;
import com.waverider.promotion.biz.manager.FrontManager;
import com.waverider.promotion.biz.service.ICache;
import com.waverider.promotion.domain.PromotionDO;
import com.waverider.promotion.domain.RebateModel;

public class FrontManagerImpl implements FrontManager {
	private PromotionDAO promotionDAO;
	private OrderDAO orderDAO;
	private ICache<Long, Object> localCacheService;

	public void setOrderDAO(OrderDAO orderDAO) {
		this.orderDAO = orderDAO;
	}

	public void setPromotionDAO(PromotionDAO promotionDAO) {
		this.promotionDAO = promotionDAO;
	}

	public void setLocalCacheService(ICache<Long, Object> localCacheService) {
		this.localCacheService = localCacheService;
	}

	@Override
	public List<PromotionDO> getPromotionByShopId(Long shopId, Long total) {
		Map<String, Long> params = new HashMap<String, Long>();
		params.put("shopId", shopId);
		params.put("total", total);
		return promotionDAO.getPromotionByShopId(params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RebateModel> getOrderByShopId(Long shopId, Long total) {
		if (localCacheService.get(shopId) != null)
			return (List<RebateModel>) localCacheService.get(shopId);

		Map<String, Long> params = new HashMap<String, Long>();
		params.put("shopId", shopId);
		params.put("total", total);
		List<RebateModel> orderDOList = orderDAO.getOrderByShopId(params);
		localCacheService.put(shopId, orderDOList, TTL);
		return orderDOList;
	}

	@Override
	public List<PromotionDO> getPromotionByShopId(Long shopId) {
		return getPromotionByShopId(shopId, GOODS_DISPLAY_TOTAL);
	}

	@Override
	public List<RebateModel> getOrderByShopId(Long shopId) {
		return getOrderByShopId(shopId, ANNOUNCEMENT_TOTAL);
	}

}
